package com.yfh.sharding.datasource.utils;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * @author zhangQiang
 * @version 1.0
 * @date 2021-03-05 14:11
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = 1;
    public static final String SUCCESS_MESSAGE = "操作成功";
    /**
     * 错误
     */
    public static final int ERROR = 0;
    public static final String ERROR_MESSAGE = "操作失败";


    /**
     * 状态码
     */
    private int code;

    /**
     * 返回内容
     */
    private String message;

    /**
     * 数据对象
     */
    private T data;

    public Result() {
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return success (SUCCESS, SUCCESS_MESSAGE, (T) null);
    }

    public static <T> Result<T> success(String message) {
        return success (SUCCESS, message, (T) null);
    }

    public static <T> Result<T> success(T obj) {
        return success (SUCCESS, SUCCESS_MESSAGE, obj);
    }

    public static <T> Result<T> success(String message, T obj) {
        return success (SUCCESS, message, obj);
    }

    public static <T> Result<T> success(int code, String message, T obj) {
        return new Result<> (code, message, obj);
    }


    public static <T> Result<T> error() {
        return error (ERROR, ERROR_MESSAGE, (T) null);
    }

    public static <T> Result<T> error(int code) {
        return error (code, ERROR_MESSAGE, (T) null);
    }

    public static <T> Result<T> error(String message) {
        return error (ERROR, message, (T) null);
    }

    public static <T> Result<T> error(String message, T object){ return error(ERROR, message, object);};

    public static <T> Result<T> error(T obj) {
        return error (ERROR, ERROR_MESSAGE, obj);
    }

    public static <T> Result<T> error(int code, String message) {
        return error (code, message, (T) null);
    }

    public static <T> Result<T> error(int code, T obj) {
        return error (code, ERROR_MESSAGE, obj);
    }

    public static <T> Result<T> error(int code, String message, T obj) {
        return new Result<> (code, message, obj);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return JSON.toJSONString (this);
    }
}
